package com.lagou.client;

import com.lagou.client.module.Student;
import com.lagou.client.module.StudentMessage;
import com.lagou.client.module.User;
import com.lagou.client.module.UserMessage;

import java.io.IOException;
import java.util.Scanner;

public class ClientView {

    private ClientInitClose clientInitClose;

    public ClientView(ClientInitClose clientInitClose) {
        this.clientInitClose = clientInitClose;
    }

    public void clientMainPage() throws IOException, ClassNotFoundException {

        while(true){
            System.out.println("        \t\t\t\t在线考试系统        ");
            System.out.print("        \t [1] 学员系统       ");
            System.out.println("        \t [2] 管理员系统    ");
            System.out.println("        \t [3] 退出系统    ");
            System.out.println("------------------------------------------------");
            System.out.println("请选择要进行的业务编号：");
            Scanner scanner = ClientScanner.getScanner();
            int choose = scanner.nextInt();
            switch (choose){
                case 1:
                    System.out.println("正在进入学员系统...");
                    break;
                case 2:
                    System.out.println("正在进入管理员系统...");
                    ManagerLogin();
                    break;
                case 0:
                    System.out.println("正在退出系统");
                    return;
                default:
                    System.out.println("输入错误，请重新选择！");
            }
        }

    }


    private void ManagerLogin() throws IOException, ClassNotFoundException {
        System.out.println("请输入管理员账号信息：");
        String name = ClientScanner.getScanner().next();
        System.out.println("请输入管理员密码信息：");
        String password = ClientScanner.getScanner().next();

        UserMessage userMessage = new UserMessage("managerCheck",new User(name,password));
        //将UserMessage类型对象通过对象输出流发送给服务器
        clientInitClose.getOos().writeObject(userMessage);
        System.out.println("客户端发送管理员信息成功！");

        //接受服务器返回的信息
        userMessage = (UserMessage) clientInitClose.getOis().readObject();

        if("success".equals(userMessage.getType())){
            System.out.println("登陆成功！");
            System.out.println("请选择功能模块:");
            System.out.print("        \t [1] 学员管理模块       ");
            System.out.println("        \t [2] 考题管理模块    ");
            Scanner scanner = ClientScanner.getScanner();
            int choose = scanner.nextInt();
            switch (choose){
                case 1:
                    System.out.println("正在进入学员管理模块...");
                    UserManeger();
                    break;
                case 2:
                    System.out.println("正在进入考题管理模块...");
                    break;
                default:
                    System.out.println("输入错误，请重新选择！");
            }
        }else{
            System.out.println("登陆失败！");
        }
    }



    private void UserManeger() throws IOException, ClassNotFoundException {
        while(true){
            System.out.println("请选择相应操作：");
            System.out.print("        \t [1] 添加学员       ");
            System.out.println("        \t [2] 修改学员    ");
            System.out.print("        \t [3] 删除学员       ");
            System.out.println("        \t [4] 查询学员    ");
            int ch = ClientScanner.getScanner().nextInt();
            switch (ch){
                case 1:
                    stdAdd();
                    break;
                case 2:
                    stdUpdate();
                    break;
                case 3:
                    stdDel();
                    break;
                case 4:
                    getStd();
                    break;
                default:
                    return;
            }
        }

    }


    /**
     * 添加学员信息
     * @throws IOException
     * @throws ClassNotFoundException
     */
    private void stdAdd() throws IOException, ClassNotFoundException {
        System.out.println("请输入学员姓名:");
        String name = ClientScanner.getScanner().next();
        System.out.println("请输入学员年龄:");
        int age = ClientScanner.getScanner().nextInt();

        StudentMessage studentMessage = new StudentMessage("stdAdd",new Student(name,age));
        clientInitClose.getOos().writeObject(studentMessage);
        System.out.println("客户端发送添加学员信息成功!");

        studentMessage = (StudentMessage) clientInitClose.getOis().readObject();
        if("success".equals(studentMessage.getType())){
            System.out.println("客户端添加学员信息成功！");
            System.out.println("学员信息：" + studentMessage.getStudent());
        }else{
            System.out.println("客户端添加学员信息失败！");
        }
    }


    /**
     * 修改学员信息
     * @throws IOException
     * @throws ClassNotFoundException
     */
    private void stdUpdate() throws IOException, ClassNotFoundException {
        System.out.println("请输入需要修改信息的学员姓名:");
        String name = ClientScanner.getScanner().next();
        System.out.println("请输入修改的学员年龄:");
        int age = ClientScanner.getScanner().nextInt();

        StudentMessage studentMessage = new StudentMessage("stdUpdate",new Student(name,age));
        clientInitClose.getOos().writeObject(studentMessage);
        System.out.println("客户端发送修改学员信息成功!");

        studentMessage = (StudentMessage) clientInitClose.getOis().readObject();
        if("success".equals(studentMessage.getType())){
            System.out.println("客户端修改学员信息成功！");
            System.out.println("修改后学员信息：" + studentMessage.getStudent());
        }else{
            System.out.println("客户端修改学员信息失败！");
        }
    }


    /**
     * 删除学员
     * @throws IOException
     * @throws ClassNotFoundException
     */
    private void stdDel() throws IOException, ClassNotFoundException {
        System.out.println("请输入需要删除的学员姓名:");
        String name = ClientScanner.getScanner().next();

        Student student = new Student();
        student.setUsername(name);

        StudentMessage studentMessage = new StudentMessage("stdDel",student);
        clientInitClose.getOos().writeObject(studentMessage);
        System.out.println("客户端发送删除学员信息成功!");

        studentMessage = (StudentMessage) clientInitClose.getOis().readObject();
        if("success".equals(studentMessage.getType())){
            System.out.println("客户端删除学员信息成功！");
        }else{
            System.out.println("客户端删除学员信息失败！");
        }
    }

    private void getStd() throws IOException, ClassNotFoundException {
        System.out.println("请输入需要查询的学员姓名:");
        String name = ClientScanner.getScanner().next();

        Student student = new Student();
        student.setUsername(name);

        StudentMessage studentMessage = new StudentMessage("getStd",student);
        clientInitClose.getOos().writeObject(studentMessage);
        System.out.println("客户端发送查询学员信息成功!");

        studentMessage = (StudentMessage) clientInitClose.getOis().readObject();
        if("success".equals(studentMessage.getType())){
            System.out.println("客户端查询学员信息成功！");
            System.out.println("学员信息：" + studentMessage.getStudent());
        }else{
            System.out.println("客户端查询学员信息失败！");
        }
    }

}
